<?php
// $Id: block.tpl.php,v 1.3 2007/08/07 08:39:36 goba Exp $
$requestUri = $_SERVER['REQUEST_URI'];

if ($block->delta == 0) {
 
 if (SITE_ID>0) {
  // Get user mapped sites
  if (user_access('View All Sites')) {
    $user_sites = load_all_sites();
  }
  else {
    $user_sites = get_site_details($user->uid);
  }
  $site_ids = array();
  if (is_array($user_sites) && count($user_sites) > 0) {
    foreach ($user_sites as $siteId => $site_Id) {
      if (user_access('View All Sites')) {
        $site_ids[$siteId] = $siteId;
      }
      else {
        $site_ids[$site_Id['SiteId']] = $site_Id['SiteId'];
      }
    }
  }
  if ($user->uid != 1 && $user->uid != "" && in_array(SITE_ID, $site_ids)) { ?>
     <div id="block-<?php print $block->module .'-'. $block->delta; ?>" class="clear-block block block-<?php print $block->module ?>">

     <?php
      if (strlen(SITE_NAME)>15) {
        $display_site_name= substr(SITE_NAME,0,15)."..";
      }
      else {
        $display_site_name= SITE_NAME;
      }
     ?>
		
    <h2><?php echo $display_site_name; ?></h2>
    <div class="content" style="overflow: auto;">
      <ul class="menu">
              <?php if (is_array($block->content['modules'])) {
                $cnt=0;
                foreach ($block->content['modules'] as $blockModules) { ?>
                  <?php
                  if (strpos($blockModules['href'],"/") !=false)
                    $modulehref = substr($blockModules['href'], 0,strpos($blockModules['href'],"/"));
                  else
                    $modulehref = substr($blockModules['href'], 0);                  
                  ?>
                  <li class="leaf <?php if(strpos($requestUri, $modulehref) !== false) {echo 'active-trail';}?>">
                      <?php if ($blockModules['name'] == 'Web Admin') {$href = $blockModules['href']; $target = 'target = "_blank"';} else {$href = base_path() . $blockModules['href']; $target = '';} ?>
                      <a title ="<?php echo $blockModules['title'];?>"  href="<?php echo $href;?>" <?php echo $target;?>><?php echo $blockModules['name'];?> </a>
                  </li>
                      <?php $cnt++;
                    }
                  }
                  ?>
      </ul>
    </div>
   </div>
  <?php        
		 }     
    }
  elseif ($_COOKIE['CURRENT_CONTENT_LEGAL_ENTITY_ID'] > 0) {     
	 if ($user->uid != 1 && $user->uid != "") { ?>
     <div id="block-<?php print $block->module .'-'. $block->delta; ?>" class="clear-block block block-<?php print $block->module ?>">
      <?php
      if ($_COOKIE['CURRENT_CONTENT_LEGAL_ENTITY_ID'] > 0) {
		
        if (strlen($_COOKIE['CURRENT_CONTENT_LEGAL_ENTITY_NAME'])>15) {
          $display_legalentity_name= substr($_COOKIE['CURRENT_CONTENT_LEGAL_ENTITY_NAME'],0,15)."..";
        }
        else {
          $display_legalentity_name= $_COOKIE['CURRENT_CONTENT_LEGAL_ENTITY_NAME'];
        }
		?>
    <h2><?php echo $display_legalentity_name; ?></h2>
    <div class="content" style="overflow: auto;">
      <ul class="menu">
              <?php if(is_array($block->content['legalentities_modules'])) {
                $cnt=0;
                foreach($block->content['legalentities_modules'] as $blockModules) { ?>
                  <?php
          if(strpos($blockModules['href'],"/") !=false)
                    $modulehref = substr($blockModules['href'], 0,strpos($blockModules['href'],"/"));
                  else
                    $modulehref = substr($blockModules['href'], 0);
                  ?>
        <li class="leaf <?php if(strpos($requestUri, $modulehref) !== false) {
                    echo 'active-trail';
                      }?>"><a href="<?php echo base_path() . $blockModules['href'];?>"><?php echo $blockModules['name'];?> </a></li>
                      <?php $cnt++;
                    }
                  }
                  ?>
      </ul>
     </div>
    </div>
    <?php
      }
     }
   }
 }
 elseif ($block->delta==1) {
  ?>
  <div id="block-<?php print $block->module .'-'. $block->delta; ?>" class="clear-block block block-<?php print $block->module ?>">
  <h2 class='hMenus'><?php echo t('Global Actions');?></h2>
    <div class="content" style="overflow: auto;">
      <ul class="menu">
          <?php foreach ($block->content as $sites) {?>
            <li class="leaf  <?php if ((strpos($requestUri, $sites['href']) !== false && arg(0) != 'site') || ((arg(0) == 'node') && ($sites['name'] == 'Legal Entities')) ||
                                          (($sites['name'] == 'User Management') && ((arg(3) == 'create') || (arg(2) == 'edit'))) || (arg(0) == 'site' && $sites['name'] == 'Legal Entities' && arg(1) != '') ||
                                          (arg(0) == 'site' && $sites['name'] == 'Site Management' && arg(1) == '')) {
                               echo 'active-trail';  if ((($sites['name'] == 'Administration') && ((arg(3) == 'create') || (arg(2) == 'edit')))){echo 'dummy';}
                    }  ?>">
              <a href="<?php echo $sites['href'];?>"><?php echo $sites['name'];?></a>
            </li>
            <?php }?>

      </ul>
    </div>
  </div>
  <?php 
  }
  elseif ($block->delta==2) {
    if (is_array($block->content['sites']) && count($block->content['sites'])>0) {
  ?>  
   <h2 class='hMenus'><?php echo t('My Sites');?></h2>    
   <div class="content" style="overflow: auto;">
    <ul class="menu">
      <div class="addnurs">
            <?php foreach ($block->content['sites'] as $siteid => $sites) {
              if (user_access('View All Sites')) {
                $site_id = $siteid;
                $site = $sites;
              }
              else {
                $site_id = $sites['SiteId'];
                $site = $sites['SiteName'];
              }
            ?>
            <h1><a href="javascript:void(0);" onclick="selectSite(<?php echo $site_id;?>, '<?php echo addslashes($site);?>');"><?php echo $site;?></a></h1>
            <?php }?>
      </div>
    </ul>
  </div>
 <br />
<?php   
   }
  if (is_array($block->content['legalentities']) && count($block->content['legalentities'])>0) { ?>
  <h2 class='hMenus'><?php echo t('My Companies');?></h2> 
  <div class="content" style="overflow: auto;">
    <ul class="menu">
      <div class="addnurs">
            <?php
            foreach ($block->content['legalentities'] as $legalentityid => $legalentities) {
              if (user_access('View All Legal Entities')) {
                $entityid = $legalentityid;
                $entity = $legalentities;
              }
              else {
                $entityid = $legalentities['LegalEntityId'];
                $entity = $legalentities['LegalEntityName'];
              }
            ?>
            <h1><a href="javascript:void(0);" onclick="selectLegalEntity(<?php echo $entityid;?>, '<?php echo addslashes($entity);?>');"><?php echo $entity;?></a></h1>
            <?php } ?>
      </div>
    </ul>
  </div>
  <br />
  <?php
  }
 }
?>